package com.ti.shangou.service.impl;

import com.ti.shangou.dao.BusinessTypeDao;
import com.ti.shangou.pojo.vo.BusinessTypeVO;
import com.ti.shangou.service.BusinessTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 作者：田宝山
 * 日期：2020/6/4
 */
@Service
public class BusinessTypeServiceImpl implements BusinessTypeService {
    @Resource
    BusinessTypeDao businessTypeDao;

    // 只查一次数据库，返回顶层类型集合并赋值子类
    public List<BusinessTypeVO> getTypes() {
        List<BusinessTypeVO> all = businessTypeDao.selectAll();
        // 找出所有顶层类型
        List<BusinessTypeVO> parents = all.stream().filter(t -> t.getParentId() == 0).collect(Collectors.toList());
        for (BusinessTypeVO p:parents){
            // 根据父类找子类
            List<BusinessTypeVO> children = all.stream().filter(t -> t.getParentId().equals(p.getTypeId())).collect(Collectors.toList());
            p.setChildren(children);
        }
        return parents;
    }
}
